﻿using Engine;
using Game;
using System.Collections.Generic;
namespace Xmod
{
	public class XmodComponentHealth : ComponentHealth, IUpdateable
	{
		public static List<InventoryItem> slotsCaches = new List<InventoryItem>();
		public new void Update(float dt)
		{
			Vector3 position = m_componentCreature.ComponentBody.Position;
			if (Health > 0f && Health < 1f)
			{
				float num = 0f;
				if (m_componentPlayer != null)
				{
					if (m_subsystemGameInfo.WorldSettings.GameMode == GameMode.Harmless)
					{
						num = 0.0166666675f;
					}
					else if (m_componentPlayer.ComponentSleep.SleepFactor == 1f && m_componentPlayer.ComponentVitalStats.Food > 0f)
					{
						num = 0.00166666671f;
					}
					else if (m_componentPlayer.ComponentVitalStats.Food > 0.5f)
					{
						num = 0.00111111114f;
					}
				}
				else
				{
					num = 0.00111111114f;
				}
				Heal(m_subsystemGameInfo.TotalElapsedGameTimeDelta * num);
			}
			if (BreathingMode == BreathingMode.Air)
			{
				int cellContents = m_subsystemTerrain.Terrain.GetCellContents(Terrain.ToCell(position.X), Terrain.ToCell(m_componentCreature.ComponentCreatureModel.EyePosition.Y), Terrain.ToCell(position.Z));
				if (BlocksManager.Blocks[cellContents] is FluidBlock || position.Y > 259f)
				{
					Air = MathUtils.Saturate(Air - dt / AirCapacity);
				}
				else
				{
					Air = 1f;
				}
			}
			else if (BreathingMode == BreathingMode.Water)
			{
				if (m_componentCreature.ComponentBody.ImmersionFactor > 0.25f)
				{
					Air = 1f;
				}
				else
				{
					Air = MathUtils.Saturate(Air - dt / AirCapacity);
				}
			}
			if (m_componentCreature.ComponentBody.ImmersionFactor > 0f && m_componentCreature.ComponentBody.ImmersionFluidBlock is MagmaBlock)
			{
				Injure(2f * m_componentCreature.ComponentBody.ImmersionFactor * dt, null, ignoreInvulnerability: false, LanguageControl.Get("ComponentHealth", "1"));
				float num2 = 1.1f + 0.1f * (float)MathUtils.Sin(12.0 * m_subsystemTime.GameTime);
				m_redScreenFactor = MathUtils.Max(m_redScreenFactor, num2 * 1.5f * m_componentCreature.ComponentBody.ImmersionFactor);
			}
			float num3 = MathUtils.Abs(m_componentCreature.ComponentBody.CollisionVelocityChange.Y);
			if (!m_wasStanding && num3 > FallResilience)
			{
				float num4 = MathUtils.Sqr(MathUtils.Max(num3 - FallResilience, 0f)) / 15f;
				if (m_componentPlayer != null)
				{
					num4 /= m_componentPlayer.ComponentLevel.ResilienceFactor;
				}
				Injure(num4, null, ignoreInvulnerability: false, LanguageControl.Get("ComponentHealth", "2"));
			}
			m_wasStanding = (m_componentCreature.ComponentBody.StandingOnValue.HasValue || m_componentCreature.ComponentBody.StandingOnBody != null);
			if ((position.Y < 0f || position.Y > 296f) && m_subsystemTime.PeriodicGameTimeEvent(2.0, 0.0))
			{
				Injure(0.1f, null, ignoreInvulnerability: true, LanguageControl.Get("ComponentHealth", "3"));
				m_componentPlayer?.ComponentGui.DisplaySmallMessage(LanguageControl.Get("ComponentHealth", "4"), Color.White, blinking: true, playNotificationSound: false);
			}
			bool num5 = m_subsystemTime.PeriodicGameTimeEvent(1.0, 0.0);
			if (num5 && Air == 0f)
			{
				float num6 = 0.12f;
				if (m_componentPlayer != null)
				{
					num6 /= m_componentPlayer.ComponentLevel.ResilienceFactor;
				}
				Injure(num6, null, ignoreInvulnerability: false, LanguageControl.Get("ComponentHealth", "5"));
			}
			if (num5 && (m_componentOnFire.IsOnFire || m_componentOnFire.TouchesFire))
			{
				float num7 = 1f / FireResilience;
				if (m_componentPlayer != null)
				{
					num7 /= m_componentPlayer.ComponentLevel.ResilienceFactor;
				}
				Injure(num7, m_componentOnFire.Attacker, ignoreInvulnerability: false, LanguageControl.Get("ComponentHealth", "5"));
			}
			if (num5 && CanStrand && m_componentCreature.ComponentBody.ImmersionFactor < 0.25f && (m_componentCreature.ComponentBody.StandingOnValue != 0 || m_componentCreature.ComponentBody.StandingOnBody != null))
			{
				Injure(0.05f, null, ignoreInvulnerability: false, LanguageControl.Get("ComponentHealth", "6"));
			}
			HealthChange = Health - m_lastHealth;
			m_lastHealth = Health;
			if (m_redScreenFactor > 0.01f)
			{
				m_redScreenFactor *= MathUtils.Pow(0.2f, dt);
			}
			else
			{
				m_redScreenFactor = 0f;
			}
			if (HealthChange < 0f)
			{
				m_componentCreature.ComponentCreatureSounds.PlayPainSound();
				m_redScreenFactor += -4f * HealthChange;
				m_componentPlayer?.ComponentGui.HealthBarWidget.Flash(MathUtils.Clamp((int)((0f - HealthChange) * 30f), 0, 10));
			}
			if (m_componentPlayer != null)
			{
				m_componentPlayer.ComponentScreenOverlays.RedoutFactor = MathUtils.Max(m_componentPlayer.ComponentScreenOverlays.RedoutFactor, m_redScreenFactor);
			}
			if (m_componentPlayer != null)
			{
				m_componentPlayer.ComponentGui.HealthBarWidget.Value = Health;
			}
			if (Health == 0f && HealthChange < 0f)
			{
				Vector3 position2 = m_componentCreature.ComponentBody.Position + new Vector3(0f, m_componentCreature.ComponentBody.BoxSize.Y / 2f, 0f);
				float x = m_componentCreature.ComponentBody.BoxSize.X;
				m_subsystemParticles.AddParticleSystem(new KillParticleSystem(m_subsystemTerrain, position2, x));
				//更改这里的逻辑
				ComponentCraftingTable componentCraftingTable = null;
				ComponentInventory componentInventory=null;
				ComponentClothing componentClothing = null;
				foreach (IInventory item in Entity.FindComponents<IInventory>())
				{
					//衣服单独处理
					if ((componentClothing = item as ComponentClothing) != null)
					{
						InventoryType inventoryType2 = InventoryType.ComponentInventory;
						inventoryType2 = InventoryType.ComponentClothing;
						slotsCaches.Add(new InventoryItem { inventoryType = inventoryType2, m_clothes=componentClothing.m_clothes });
						continue;
					}
					for (int i=0;i<item.SlotsCount; i++) {
						InventoryType inventoryType = InventoryType.ComponentInventory;
						if (item.GetSlotCount(i) == 0 || item.GetSlotValue(i) == 0 || item.GetSlotCount(i) == 9999) continue;
						if ((componentCraftingTable = item as ComponentCraftingTable) != null)
						{
							inventoryType = InventoryType.ComponentCraftingTable;
							slotsCaches.Add(new InventoryItem { inventoryType = inventoryType, Index = i, Count = item.GetSlotCount(i), Value = item.GetSlotValue(i) });
						}
						else if((componentInventory = item as ComponentInventory) != null)
						{
							slotsCaches.Add(new InventoryItem { inventoryType = inventoryType, Index = i, Count = item.GetSlotCount(i), Value = item.GetSlotValue(i) });
						}
					}
				}
				DeathTime = m_subsystemGameInfo.TotalElapsedGameTime;
			}
			if (Health <= 0f && CorpseDuration > 0f && m_subsystemGameInfo.TotalElapsedGameTime - DeathTime > (double)CorpseDuration)
			{
				m_componentCreature.ComponentSpawn.Despawn();
			}
		}
	}
}
